import csv
from matplotlib import pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates
filename = 'Chapter16-Downloading-Data\sitka_weather_2014.csv'

with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    #使用enumerate将其索引和列打印出来
    # for index, column_header in enumerate(header_row):
    #     print(index, column_header)
    highs, dates, lows = [], [], []

    for row in reader:
        current_date = datetime.strptime(row[0], '%Y-%m-%d')
        dates.append(current_date)
        highs.append(int(row[1]))
        lows.append(int(row[3]))

    print(highs)

fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha = 0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)

plt.title('Daily high temperature - 2014', fontsize=24)
plt.xlabel(' ', fontsize=5)
plt.ylabel('Temperature(F)', fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=5)

# 设置 x 轴刻度和标签
plt.gca().xaxis.set_major_locator(mdates.MonthLocator())  # 每月一个主刻度
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))  # 日期格式
plt.gcf().autofmt_xdate()  # 自动旋转 x 轴标签以避免重叠

plt.show()
